package com.restaurant.web.android.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.restaurant.domain.Tableorder;
import com.restaurant.domain.Tables;
import com.restaurant.domain.Tableshape;
import com.restaurant.service.TablesService;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

@Component
@RequestMapping("/tablelist.do")
public class TablesListController {

	@Autowired
	private TablesService objTablesService;
	
	@RequestMapping(method=RequestMethod.GET)
	public void tableListGETRequest(ModelMap map, HttpServletRequest request, HttpServletResponse response){
		
		XStream objXStream = new XStream(new DomDriver());
		objXStream.alias("list", List.class);
		objXStream.alias("tables", Tables.class);
		objXStream.alias("tableorder", Tableorder.class);
		try {
			String xml = objXStream.toXML(objTablesService.loadByHql("from Tables where isdeleted = false"));
			response.getWriter().print(xml);
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public void tableListPOSTRequest(ModelMap map, HttpServletRequest request, HttpServletResponse response){
		
		XStream objXStream = new XStream(new DomDriver());
		objXStream.alias("Tables", Tables.class);
		objXStream.alias("Tableshape", Tableshape.class);
		try {
			response.getWriter().print(objXStream.toXML(objTablesService.addOrModify((Tables)objXStream.fromXML(request.getParameter("Tables")))));
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
